package com.sci99.ex.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.sci99.ex.model.Note;
import com.sci99.ex.util.MyBatisUtil;

public class NoteService {

	static Logger log = Logger.getLogger(NoteService.class);

	public static Note getNoteById(Long id) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Note n = s.selectOne("getNoteById", id);
		s.close();
		return n;
	}

	public static Note getNote(Long uid, Long cid) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> args = new HashMap<>(4, 1);
		args.put("employee_id", uid);// 用户
		args.put("course_id", cid);// 课程
		Note n = s.selectOne("getNote", args);
		return n;
	}

	public static Note getNote(Long uid, Long cid, Long aid) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> args = new HashMap<>(4, 1);
		args.put("employee_id", uid);// 用户
		args.put("course_id", cid);// 课程
		args.put("chapter_id", aid);// 章节
		Note n = s.selectOne("getNote", args);
		s.close();
		return n;
	}

	public static void saveNote(Note n) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		if (n.getId() == null)
			s.insert("addNote", n);
		else
			s.update("uptNote", n);
		s.commit();
		s.close();
	}
}
